package com.woyowodaows.activity.home;

import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.woyowodaows.R;
import com.woyowodaows.adapter.home.ShowPicturePagerAdapter;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.bean.mine.ImageUrlBean;
import com.woyowodaows.utils.DisplayUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 查看图片
 */
public class ShowPictureActivity extends BaseActivity {

    @BindView(R.id.iv_title_left)
    ImageView mIvTitleLeft;
    @BindView(R.id.tv_title_content)
    TextView mTvTitleContent;
    @BindView(R.id.ll_title)
    RelativeLayout mLlTitle;
    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.tv_text_title)
    TextView mTvTextTitle;
    @BindView(R.id.tv_text_content)
    TextView mTvTextContent;
    @BindView(R.id.ll_text_wz)
    LinearLayout llTextWz;


    private ShowPicturePagerAdapter mAdapter;
    private Boolean step;//判断是否是服务步骤
    private int position;

    private  List<ImageUrlBean>  imags;



    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        initTitle();
        hideTitle();
        DisplayUtils.setTransparencyStatusBar(mContext,false,R.color.black);

    }

    protected void initTitle() {
        position = getIntent().getIntExtra("position", 0); //当前下标
        imags = (List<ImageUrlBean>)getIntent().getSerializableExtra("imageUrls");
        //服务步骤对象集合
        if (null != imags) {
            step = false;
            llTextWz.setVisibility(View.GONE);
        } else if (imags != null) {
            step = true;
            llTextWz.setVisibility(View.VISIBLE);
        } else {
            new RuntimeException("请传入图片url集合或服务步骤对象集合");
            return;
        }
        mAdapter = new ShowPicturePagerAdapter(this);
        mAdapter.setImageViewOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (step) {
//                    if (mLlTitle.getVisibility() == View.VISIBLE) {
//                        mLlTitle.setVisibility(View.GONE);
//                        llTextWz.setVisibility(View.GONE);
//                    } else {
//                        mLlTitle.setVisibility(View.VISIBLE);
//                        llTextWz.setVisibility(View.VISIBLE);
//                    }
//                } else {
////                    if (mLlTitle.getVisibility() == View.VISIBLE) {
////                        mLlTitle.setVisibility(View.GONE);
////                    } else {
////                        mLlTitle.setVisibility(View.VISIBLE);
////                    }
//                    llTextWz.setVisibility(View.VISIBLE);
//                    mLlTitle.setVisibility(View.VISIBLE);
//                }
            }
        });
        mAdapter.setData(imags);
        mViewpager.setAdapter(mAdapter);
        mViewpager.setCurrentItem(position);
        mTvTitleContent.setText(mViewpager.getCurrentItem() + 1 + "/" + imags.size());
        if(imags.size()>0){
            llTextWz.setVisibility(View.VISIBLE);
            mTvTextTitle.setText(imags.get(0).getTitle());
            mTvTextContent.setText(imags.get(0).getContens());
        }
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mTvTitleContent.setText(position + 1 + "/" + imags.size());
//                if (step) {
                    mTvTextTitle.setText(imags.get(position).getTitle());
                    mTvTextContent.setText(imags.get(position).getContens());
//                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });


    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_show_picture;
    }


    @OnClick({R.id.viewpager, R.id.iv_title_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finishActivity();
                break;
            case R.id.viewpager:
                if (step) {
                    if (mLlTitle.getVisibility() == View.VISIBLE) {
                        mLlTitle.setVisibility(View.GONE);
                        llTextWz.setVisibility(View.GONE);
                    } else {
                        mLlTitle.setVisibility(View.VISIBLE);
                        llTextWz.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mLlTitle.getVisibility() == View.VISIBLE) {
                        mLlTitle.setVisibility(View.GONE);
                    } else {
                        mLlTitle.setVisibility(View.VISIBLE);
                    }
                }
                break;
        }

    }
    //关闭登录界面
    public void finishActivity() {
        DisplayUtils.closeKeybord(mIvTitleLeft, this);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
                overridePendingTransition(0, R.anim.anim_activity_close);
            }
        }, 300);
    }

}
